package cn.kgc.music.controller;

import cn.kgc.music.common.RandomNumber;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.swing.text.html.FormSubmitEvent;
import java.rmi.ServerException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/login")
public class LoginController {
    @RequestMapping("/send.do")
    @ResponseBody
    public Map<String,Object> send(String phone, HttpSession session){
        int code=Integer.parseInt(RandomNumber.randomsix());
        Map<String,Object> map=new HashMap<String,Object>();
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4FtDxU5f1C1816o1qYkW", "li8hdaiUN52Ju7b7ylmRKjEj8DHY6m");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "风流倜傥");
        request.putQueryParameter("TemplateCode", "SMS_175584267");
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            session.setAttribute("code",code);
            map.put("msg","ok");
            System.out.println(response.getData());
        } catch (com.aliyuncs.exceptions.ServerException e) {
            map.put("msg",e.getMessage());
            e.printStackTrace();
        } catch (ClientException e) {
            map.put("msg",e.getMessage());
            e.printStackTrace();
        }
        return map;
    }
    @RequestMapping("/login.do")
    @ResponseBody
    public Map<String,Object> login(int code,HttpSession session){
        Map<String,Object> map=new HashMap<String,Object>();
        System.out.println(code);
        System.out.println(session.getAttribute("code"));
        if(code!=0){
            if(session.getAttribute("code").equals(code)){
                map.put("msg","cg");
            }else{
                map.put("msg","sb");
            }
        }
        return map;
    }
}
